<?php
function htmlspec($src) {
	return htmlspecialchars($src, ENT_COMPAT, 'UTF-8');
}

function make_blog_url($blogger_id, $user_url) {
	if(is_null($user_url) || strlen($user_url) == 0) {
		return '/' . $blogger_id;
	} else {
		return '/' . $user_url;
	}
}

function get_avatar_name($user_id, $rand = FALSE) {
	$fn = $_SERVER['DOCUMENT_ROOT'] . '/img/avatars/avt_' . $user_id . '_48x48.jpg';
	if(file_exists($fn)) {
		if($rand) {
			return 'avt_' . $user_id . '_48x48.jpg?R=' . rand();
		} else {
			return 'avt_' . $user_id . '_48x48.jpg';
		}
	} else {
		return 'avt_-1_48x48.jpg';
	}
}

function date_part($str_datetime) {
	$dp = date_parse($str_datetime);
	return $dp['month'] . '/' . $dp['day'] . '/' . $dp['year'];
}

function entry_state_indicator($entry_state, $smaller = FALSE) {
	$r = ' ';
	switch($entry_state) {
		case 0:
			$r .= 'secret';
			break;
		case 3:
			$r .= 'community';
			break;
		case 99:
			$r .= 'draft';
			break;
		default:
			$r = '';
			break;
	}
	if($smaller && (strlen($r) >= 1)) {
		$r .= '_small';
	}
	return $r;
}

function short_content($content) {
	return (mb_strlen($content, 'UTF-8') > 128) ? mb_substr($content, 0, 128, 'UTF-8') . '...' : $content;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<title><?php echo(htmlspec($blog_info->title)); ?></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/css/iphone.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/ubb_more.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/expressions.css" media="screen" />
</head>
<body>
	<div id="wrapper">
		<div id="hdr1">
			<img alt="" class="userAvatar" src="/img/avatars/<?php echo(get_avatar_name($blog_info->id)); ?>" />
			<h1 class="fadeLong"><?php echo(htmlspec($blog_info->title)); ?></h1>
			<div id="operMenuHandle"><a href="javascript:;">Menu▼</a></div>
			<ul id="operMenu">
				<li><a href="/">返回社区</a></li>
				<li><a href="<?php echo('/' . $blogger_uri); ?>">Blog 首页</a></li>
				<?php
				if($is_blog_owner) {
				?>
				<li><a id="lnkPostNewEntry" href="javascript:;">发表新网志</a></li>
				<li><a href="<?php echo('/' . $blogger_uri); ?>/mgmt/logout">Logout</a></li>
				<?php
				} else {
				?>
				<li><a href="<?php echo('/' . $blogger_uri); ?>/mgmt/login">登录</a></li>
				<?php
				}
				?>
			</ul>
		</div>
		<div id="bdy1">
		
			<?php foreach($recent_entries as $entry) { ?>
			
			<div class="entry">
				<h3 class="expr_<?php echo($entry->expression); ?>">
					<?php if($entry->id > 0) { ?>
					<a href="<?php echo('/' . $blogger_uri . '/entry/' . $entry->id); ?>"><?php echo(htmlspec($entry->title)); ?></a>
					<?php } else { ?>
					<?php echo($entry->state == 0 ? '保密网志' : '社区内部网志'); ?>
					<?php } ?>
				</h3>
				
				<div class="content<?php echo(entry_state_indicator($entry->state)); ?>">
					<?php echo($entry->content); ?>
				</div>
				
				<div class="info">
					发布于 <?php echo($entry->post_time); ?>
					|
					<?php if($entry->id > 0) { ?>
					<a href="<?php echo('/' . $blogger_uri . '/entry/' . $entry->id . '#comments'); ?>">评论：<strong><?php echo($entry->comment_num); ?></strong></a>
					<?php } else { echo('评论：N/A'); } ?>
				</div>
			</div>
			
			<?php } ?>
		
		</div>
		<?php
		include('common_footer.php');
		?>
	</div>
	<?php
	if($is_blog_owner) {
	?>
	<div id="newEntryPopup">
		<div id="closeBox"><a href="javascript:;">X</a></div>
		<form>
			<label for="txtEntryTitle">网志标题：</label>
			<input type="text" id="txtEntryTitle" maxlength="128" class="textBox" />
			<label for="txtEntryContent">网志正文：</label>
			<textarea id="txtEntryContent" class="textBox" rows="7"></textarea>
			<label>网志类别：</label>
			<select id="selEntryCates" class="textBox">
				<option value="1">未分类</option>
				<?php foreach($custom_cates as $cc) { ?>
				<option value="<?php echo($cc->id); ?>"><?php echo(htmlspec($cc->title)); ?></option>
				<?php } ?>
			</select>
			<input type="button" id="btnPublish" class="button" value="发布网志" />
			<img id="ajaxLoading" alt="Loading" src="/img/ajax-loader.gif" border="0" width="43" height="11" style="vertical-align:middle;display:none;" />
		</form>
	</div>
	<?php
	}
	?>
<!-- JS -->
<script type="text/javascript" src="/js/3rd/jquery.pack.js"></script>
<script type="text/javascript" src="/js/3rd/json2.js"></script>
<script type="text/javascript" src="/js/iphone.js"></script>
<script type="text/javascript">
var g_userSiteRoot = "<?php echo('/' . $blogger_uri); ?>";

var currentEntryId = 0;
var toCreate = true;
var published = false;
var postEntryData = {
	"id": 0,
	"expression": "1",
	"title": "",
	"category_id": 1,
	"allow_comment": true,
	"state": 1,
	"tags": "",
	"content": ""
};

function clientValidated(loudly) {
	var entryTitle = $("#txtEntryTitle").val();
	if(entryTitle == "") {
		if(loudly) alert("无论如何也请写上标题，谢谢！");
		return false;
	}
	postEntryData.title = entryTitle;
	
	var entryContent = $("#txtEntryContent").val();
	if(entryContent == "") {
		if(loudly) alert("什么内容都没有？");
		return false;
	}
	postEntryData.content = entryContent;
	
	return true;
}

function doSaveEntryCalback(rdata) {
	$("#ajaxLoading").hide();
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	if(toCreate) {
		currentEntryId = rdata[1];
		postEntryData.id = rdata[1];
		toCreate = false; // from now on, updating
	}
	if(published) {
		location.href = g_userSiteRoot;
	}
}

function doSaveEntry() {
	postEntryData.category_id = $("#selEntryCates").val();
	
	var postUrl = g_userSiteRoot + "/mgmt/edit_entry";
	$("#ajaxLoading").show();
	$.post(postUrl, {
		"hidIsPostBack": "ja",
		"posted_data": JSON.stringify(postEntryData)
	}, doSaveEntryCalback);
}

$(document).ready(function() {
	initPage4iPhone();
	
	$("#operMenuHandle").click(function() {
		$("#operMenu").toggle();
	});
	
	$("#lnkPostNewEntry").click(function() {
		$("#operMenu").hide();
		$("#newEntryPopup").toggle();
	});
	
	$("#closeBox a").click(function() {
		$("#newEntryPopup").hide();
	});
	
	$("#btnPublish").click(function() {
		if(clientValidated(true)) {
			if(confirm("确定要发布？")) {
				published = true;
				doSaveEntry();
			}
		}
	});
});
</script>
<!-- /JS -->
</body>
</html>